<?php

/**
 * Created by PhpStorm.
 * User: zhang
 * Date: 2019/1/1
 * Time: 18:52
 */

namespace app\admin\model;

use think\Model;

/**
 * Class Reback
 * @package app\admin\model
 * 退款模型
 */
class Reback extends Model {
    /**
     * @param $pageNum
     * @param $numPerPage
     * @param $order_sn
     * @param $status
     * @param $company_id
     * @return mixed
     * 获取列表
     */
    public function getList($pageNum, $numPerPage, $order_sn, $status, $company_id) {
        $map = [];
        if ($company_id) {
            $map['company_id'] = $company_id;
        }
        if (!empty($order_sn)) {
            $map['order_sn'] = ['like', "%{$order_sn}%"];
        }
        if($status != -1 && $status != ''){
            $map['refund_status'] = $status;
        }else{
            $map['refund_status'] = ['in','1,2,3,4,5'];
        }
        $list = db('order')
                ->where($map)
                ->page($pageNum, $numPerPage)
                ->order('create_time desc')
                ->select();
        return $list;
    }

    /**
     * @param $order_sn
     * @param $status
     * @param $company_id
     * @return int|string
     */
    public function getCount($order_sn, $status, $company_id) {
        $map = [];
        if (!empty($order_sn)) {
            $map['order_sn'] = ['like', "%{$order_sn}%"];
        }
        if ($company_id) {
            $map['company_id'] = $company_id;
        }
		//if($status != ''){
		//	$map['status'] = $status;
		//}
		if($status != -1 && $status != ''){
            $map['refund_status'] = $status;
        }else{
            $map['refund_status'] = ['in','1,2,3,4,5'];
        }
        $count = db('order')
                ->where($map)
                ->count();
        return $count;
    }
    /**
     * @param $reback_id
     * 平台同意退款
     */
    public function doagreereback($reback_id) {
        //改订单状态
        $dotime = time();
        $map = [
            'refund_status' => 2,
            'order_status' => 4,
            'update_time' => $dotime,
            'cancel_time' => $dotime,
        ];
        $map2 = [
            'order_id' => $reback_id,
            'refund_status' => 2,
            'do_time' => time()
        ];
        db('refund_log')->insert($map2);
        db('order')->where('order_id', $reback_id)->update($map);
        $subscribe_id = db('order')->where('order_id', $reback_id)->value('subscribe_id');
        if($subscribe_id){
            db('subscribe')->where('id', $subscribe_id)->setInc('num');
        }
    }
    /**
     * @param $reback_id
     * @param $open_id
     * @throws \Exception
     * 平台确认退款
     */
    public function dosurereback($reback_id,$open_id) {
        //改订单状态
        db('order')->where('order_id', $reback_id)->setField('refund_status', 3);
        db('order')->where('order_id', $reback_id)->setField('update_time', time());
//        $map2 = [
//            'order_id' => $reback_id,
//            'refund_status' => 3,
//            'do_time' => time()
//        ];
//        db('refund_log')->insert($map2);
        //生成消息记录
        $mapa = array(
            'order_id' => $reback_id,
            'refund_status' => 3,
            'do_time' => time()
        );
        $result_message = db('refund_log')->insert($mapa);
        if (!$result_message) {
            exception('添加失败message');
        }
    }
    /**
     * @param $reback_id
     * @param $open_id
     * @param $refuse_case
     * @throws \Exception
     * 平台拒绝退款
     */
    public function dorefusereback($reback_id,$open_id,$refuse_case) {
        if(empty($refuse_case)){
            exception('请填写拒绝退款原因');
        }
        //改订单状态
        db('order')->where('order_id', $reback_id)->setField('refund_status', 5);
        db('order')->where('order_id', $reback_id)->setField('refuse_case', $refuse_case);
        db('order')->where('order_id', $reback_id)->setField('update_time', time());
        //生成消息记录
        $mapa = array(
            'order_id' => $reback_id,
            'refund_status' => 5,
            'do_time' => time()
        );
        $result_message = db('refund_log')->insert($mapa);
        if (!$result_message) {
            exception('添加失败message');
        }
    }
}
